﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Chromatics.Pages.Tiles;
using System.Windows.Media;
using Chromatics.Resources;
using System.Windows.Resources;
using Chromatics.Pages.CustomMessageBox;

namespace Chromatics
{
    public partial class LevelChoosePage : PhoneApplicationPage
    {
        private List<Tile> easyTileList;
        private List<Tile> mediumTileList;
        private List<Tile> hardTileList;

        public LevelChoosePage()
        {
            InitializeComponent();
            this.easyTileList = new List<Tile>();
            this.mediumTileList = new List<Tile>();
            this.hardTileList = new List<Tile>();
            this.reloadTiles();
        }

        private void tempLevelOneTile_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            string str = ((Tile)sender).Name;
            Difficulty difficulty = Difficulty.Easy;
            int levelNumber = int.Parse(str.Split(' ')[1]);
            GameLogic.CurrentLevelNumber = int.Parse(str.Split(' ')[1]);
            if (str[0] == 'E')
            {
                difficulty = Difficulty.Easy;
            }
            else if (str[0] == 'M')
            {
                difficulty = Difficulty.Medium;
            }
            else if (str[0] == 'H')
            {
                difficulty = Difficulty.Hard;
            }
            if (((Tile)sender).GetStatus() != LevelStatus.Closed)
            {
                SoundPlayer.PlayDing();
                //((Tile)sender).Run();
                GameLogic.CurrentLevelNumber = levelNumber;
                GameLogic.CurrentDifficulty = difficulty;
                NavigationService.Navigate(new Uri("/Pages/GamePage.xaml", UriKind.Relative));
            }
            else
            {
                SoundPlayer.PlayLock();

                Pages.LevelLockPage.setCurrent(levelNumber, difficulty);
                NavigationService.Navigate(new Uri("/Pages/LevelLockPage.xaml", UriKind.Relative));
            }

        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.setTiles();
        }

        private void setTiles()
        {
            for (int i = 0; i < this.easyTileList.Count; i++)
            {
                if (DataLoader.Instance.LoadLevelInfo(i + 1, Difficulty.Easy).Status != this.easyTileList[i].GetStatus())
                {
                    this.easyTileList[i].SetStatus(DataLoader.Instance.LoadLevelInfo(i + 1, Difficulty.Easy).Status);
                }
            }

            for (int i = 0; i < this.mediumTileList.Count; i++)
            {
                if (DataLoader.Instance.LoadLevelInfo(i + 1, Difficulty.Medium).Status != this.mediumTileList[i].GetStatus())
                {
                    this.mediumTileList[i].SetStatus(DataLoader.Instance.LoadLevelInfo(i + 1, Difficulty.Medium).Status);
                }
            }

            for (int i = 0; i < this.hardTileList.Count; i++)
            {
                if (DataLoader.Instance.LoadLevelInfo(i + 1, Difficulty.Hard).Status != this.hardTileList[i].GetStatus())
                {
                    this.hardTileList[i].SetStatus(DataLoader.Instance.LoadLevelInfo(i + 1, Difficulty.Hard).Status);
                }
            }
        }

        private void reloadTiles()
        {

            int count = 1;
            SolidColorBrush blueBrush = new SolidColorBrush() { Color = new Color() { A = 0xFF, R = 0x51, G = 0x28, B = 0xF7 } };
            SolidColorBrush greenBrush = new SolidColorBrush() { Color = new Color() { A = 0xFF, R = 0x25, G = 0xF6, B = 0x41 } };
            SolidColorBrush redBrush = new SolidColorBrush() { Color = new Color() { A = 0xFF, R = 0xF6, G = 0x25, B = 0x25 } };
            int easyCount = DataLoader.Instance.LevelsCount(Difficulty.Easy);
            int mediumCount = DataLoader.Instance.LevelsCount(Difficulty.Medium);
            int hardCount = DataLoader.Instance.LevelsCount(Difficulty.Hard);

            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    Tile tile;
                    if (easyCount >= count)
                    {
                        tile = new Tile() { TileMargin = new Thickness(j * 85, i * 85, 0, 0), TileContent = count.ToString(), Name = "E " + count.ToString(), TileBorderBrush = greenBrush };
                        tile.Tap += tempLevelOneTile_Tap;
                        tile.MouseEnter+=tile_MouseEnter;
                        tile.SetStatus(DataLoader.Instance.LoadLevelInfo(count, Difficulty.Easy).Status);
                        this.EasyGrid.Children.Add(tile);
                        this.easyTileList.Add(tile);
                    }

                    if (mediumCount >= count)
                    {
                        tile = new Tile() { TileMargin = new Thickness(j * 85, i * 85, 0, 0), TileContent = count.ToString(), Name = "M " + count.ToString(), TileBorderBrush = blueBrush };
                        tile.Tap += tempLevelOneTile_Tap;
                        tile.SetStatus(DataLoader.Instance.LoadLevelInfo(count, Difficulty.Medium).Status);
                        this.NormalGrid.Children.Add(tile);
                        this.mediumTileList.Add(tile);
                    }

                    if (hardCount >= count)
                    {
                        tile = new Tile() { TileMargin = new Thickness(j * 85, i * 85, 0, 0), TileContent = count.ToString(), Name = "H " + count.ToString(), TileBorderBrush = redBrush };
                        tile.Tap += tempLevelOneTile_Tap;
                        tile.SetStatus(DataLoader.Instance.LoadLevelInfo(count, Difficulty.Hard).Status);
                        this.HardGrid.Children.Add(tile);
                        this.hardTileList.Add(tile);
                    }

                    count++;
                }
            }
        }

        void tile_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            ((Tile)sender).Run();
        }

        private void PhoneApplicationPage_BackKeyPress_1(object sender, System.ComponentModel.CancelEventArgs e)
        {
            SoundPlayer.PlayDing();
        }
    }
}